/**
 * 账户历史管理初始化
 */
var AccountHistory = {
    id: "AccountHistoryTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
AccountHistory.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '商户号', field: 'mchNo', visible: true, align: 'center', valign: 'middle'},
            {title: '账户编号', field: 'accountNo', visible: true, align: 'center', valign: 'middle'},
            {title: '交易类型', field: 'transType', visible: true, align: 'center', valign: 'middle',formatter:"transTypeFormatter"},
            {title: '交易流水号', field: 'transFlowNo', visible: true, align: 'center', valign: 'middle'},
            {title: '交易时间', field: 'transTime', visible: true, align: 'center', valign: 'middle'},
            {title: '入账时间', field: 'accountingTime', visible: true, align: 'center', valign: 'middle'},
            {title: '金额', field: 'amount', visible: true, align: 'center', valign: 'middle'},
            {title: '上次余额', field: 'lastBalance', visible: true, align: 'center', valign: 'middle'},
            {title: '当前金额', field: 'banlance', visible: true, align: 'center', valign: 'middle'},
            {title: '资金方向', field: 'fundDirection', visible: true, align: 'center', valign: 'middle',formatter:"fundDirectionFormatter"},
            {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
    ];
};

//交易类型格式化
function transTypeFormatter(value, row, index){
	if (value == 100) {
		return '线上支付';
	} else if(value == 101) {
		return '线下充值';
	}else if(value == 102){
		return '线上代付';
	}else if(value == 103){
		return '线下体现';
	}
}

//资金风向格式化
function fundDirectionFormatter(value, row, index){
	if (value == 123) {
		return '<span class="label label-success">增加</span>';
	} else if(value == 321){
		return '<span class="label label-danger">扣减</span>';
	}
}



/**
 * 检查是否选中
 */
AccountHistory.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        AccountHistory.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加账户历史
 */
AccountHistory.openAddAccountHistory = function () {
    var index = layer.open({
        type: 2,
        title: '添加账户历史',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/accountHistory/accountHistory_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看账户历史详情
 */
AccountHistory.openAccountHistoryDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '账户历史详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/accountHistory/accountHistory_update/' + AccountHistory.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除账户历史
 */
AccountHistory.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/accountHistory/delete", function (data) {
            Feng.success("删除成功!");
            AccountHistory.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("accountHistoryId",this.seItem.id);
        ajax.start();
    }
};


AccountHistory.formParams = function() {
    var queryData = {};
    queryData['mchNo'] = $("#mchNo").val();
    queryData['accountNo'] = $("#accountNo").val();
    queryData['transType'] = $("#transType").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();

    return queryData;
}

/**
 * 查询交易流水列表
 */
AccountHistory.search = function () {
	AccountHistory.table.refresh({query: AccountHistory.formParams()});
};

$(function () {
    var defaultColunms = AccountHistory.initColumn();
    var table = new BSTable(AccountHistory.id, "/accountHistory/list", defaultColunms);
    table.setPaginationType("server");//服务器端分页
    table.setQueryParams(AccountHistory.formParams());
    AccountHistory.table = table.init();
});
